/**
 * Copyright (C) 2006 OQube 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *  
 * author: StringTemplate
 * created: $date$
 * source: $from$
 */
package speculoos.jndi.mappers;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;

import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.MapUtils;

import fr.lifl.utils.TypeHelper;

import speculoos.core.Mapper;
import speculoos.core.MapperException;
import $bean.beanDescriptor.beanClass.name$;

/**
 * This mapper creates an {@see javax.naming.directory.Attributes} object from a
 * Java object of type $bean.beanDescriptor.class.name$.
 * Note that everything gets converted to String using method {@see #convert(Object,Object,Class)}
 * method. By default, this method returns the result of invoking 
 * {@see java.lang.Object#toString()}.
 *  
 * @author nono
 * 
 */
public class AttributesFrom$simpleName$ implements Mapper {

	/**
	 * This method uses reflection through the {@see fr.lifl.utils.TypeHelper}
	 * class. On successful completion, it returns an instance of Attributes
	 * that may later be used for input to other mappers.
	 * 
	 * @see speculoos.core.Mapper#map(java.lang.Object, java.util.Map)
	 */
	public Object map(Object input, Map param) throws MapperException {
		Attributes attributes = new BasicAttributes($caseIgnore$);
		Attribute attribute = null;
		Object o = null;
		$simpleName$ bean = ($simpleName$) input;
        $scalars:{$if(map.(it.name))$$templates/mappers/propertygetScalar(prop=it,mapto=map.(it.name))$$\n$$endif$}$
		$primitives:{$if(map.(it.name))$$templates/mappers/propertygetPrim(prop=it,mapto=map.(it.name))$$\n$$endif$}$
		$containers:{$if(map.(it.name))$$templates/mappers/propertygetContainer(prop=it,mapto=map.(it.name))$$\n$$endif$}$
		return attributes;
	}

	/**
	 * Convert the value of the given property to something suitable
	 * for input to a LDAP query.
	 * By default, this method returns the string representation of the
	 * given object.
	 *  
	 * @param p the property to convert.
	 * @param o the value of property to convert.
	 * @return the converted value.
	 */
	public Object convert(PropertyDescriptor p, Object o) {
		return (o == null) ? null : o.toString();
	}

	/*
	 * @see speculoos.core.Mapper#getName()
	 */
	public String getName() {
		return "MapFrom$simpleName$";
	}

}
